const query = require('../../db/db')
//对密码进行加密
const bcrypt = require('bcryptjs')
const Email = require('../../sendEmail/sendEmail')
const moment = require('moment')
//token 包
const jwt = require('jsonwebtoken')
//导入密钥全局配置文件
const config = require('../../config')
//产生验证码作为全局变量
let yzm = Math.random().toFixed(6).slice(-6)
//注册
exports.loginRe = async (req, res) => {
    try {
        console.log("这是验证码", yzm);
        const userinfo = req.body
        console.log(userinfo);
        const sql = 'select * from ev_users_email where email=?'
        let result = await query(sql, [userinfo.email])
        if (result.length > 0) {
            return res.send({
                status: 0,
                data: '该账号已被注册',
            })
        }
        //TODO: 用户名可用使用
        if (userinfo.yzm !== yzm) {
            return res.send({
                status: 1,
                data: '验证码错误'
            })
        }
        userinfo.password = bcrypt.hashSync(userinfo.password, 10)
        const loginSql = 'insert into ev_users_email set ?'
        let loginResult = await query(loginSql, {
            email: userinfo.email,
            password: userinfo.password,
            nickname: userinfo.email,//昵称 默认为email
            sex: '外星人',
            createemail: moment().format('YYYY-MM-DD HH:mm:ss'),//创建时间
            logintime: moment().format('YYYY-MM-DD HH:mm:ss'),//最近登录时间
        })
        console.log(loginResult);
        if (loginResult.affectedRows !== 1) {
            return res.send({
                status: 1,
                data: '注册用户失败,请稍后再试!!!'
            })
        }
        res.send({
            status: 0,
            data: '注册成功',
        })
    } catch (error) {
        console.log(error);
        res.send({
            status: 1,
            data: '数据库异常',
        })
    }
}
//登录
exports.logins = async (req, res) => {
    try {
        const userinfo = req.body
        const sql = 'select * from ev_users_email where email=?'
        let result = await query(sql, [userinfo.email])
        if (result.length !== 1) {
            return res.send({
                status: 1,
                data: '该账号不存在',
            })
        }
        //已查到该账户  判断密码是否正确
        const compareResult = bcrypt.compareSync(userinfo.password, result[0].password)
        if (!compareResult) {
            return res.send({
                status: 1,
                data: '登录失败'
            })
        }
        //修改最近登录时间
        const upSql = 'update ev_users_email  set logintime=? where email=?'
        const updResut = await query(upSql, [moment().format('YYYY-MM-DD HH:mm:ss'), userinfo.email])
        if (updResut.affectedRows !== 1) {
            console.log('修改最近登录时间失败');
        }
        //在服务器端生成token
        const user = { ...result[0], password: '', }
        const tokenStr = jwt.sign(user, config.jwtSecretKey, { expiresIn: config.expiresIn })
        res.send({
            status: 0,
            data: '登录成功',
            token: 'Bearer ' + tokenStr
        })
    } catch (error) {
        console.log(error);
        res.send({
            status: 1,
            data: error
        })
    }

}
exports.sendEnail = (req, res) => {
    try {
        Email('2035270689@qq.com', yzm)
        res.send({
            status: 0,
            data: yzm
        })
    } catch (error) {
        res.send({
            status: 1,
            data: error
        })
    }
}